<?php
require('init.php');
 
//vars
$generValue = '';
$cityValue = '';
$sizeValue = '';
$tipoidenValue = '';
$uniValue = '...';
//$username = "";
$clasc = "noshow";
$clasn = "noshow";
$claspa = "noshow";
$classa = "noshow";
$clasu = "noshow";
$clasw = "noshow";
$claswr = "noshow";
$clasi = "noshow";
$clasd = "noshow"; 
$clasg = "noshow"; 
$clast = "noshow"; 
$clasdir = "noshow"; 
$clase = "noshow";
$clasp = "noshow";
$clascond = "noshow";
if ( isset( $_GET ) ){
	if( isset( $_GET['res'] ) ){
		$answer = $_GET['res'];
		$answer=array_recibe($answer);
		//echo $array[0];
//recojo answer
		$city = $answer[0];
		$name = $answer[1];
		$fname = $answer[2];
		$sname = $answer[3];
		$user  = $answer[4];
		$passw  = $answer[5];
		$tident = $answer[6];
		$ident = $answer[7];
		$genero= $answer[8];
		$dir = $answer[9];
		$tel = $answer[10];
		$email = $answer[11];
		$programs = $answer[12];
		$condiciones = $answer[13];
	}
	if( isset( $_GET['v'] ) ){
		$values = $_GET['v'];
		$values=array_recibe($values);
		//recojo values
		$cityValue = $values[0];
		$nameValue = $values[1];
		$papellValue = $values[2];
		$sapellValue = $values[3];
		$usernameValue = $values[4];
		$tipoidenValue = $values[5];
		$identifValue = $values[6];
		$generValue = $values[7];
		$dirValue = $values[8];
		$telValue = $values[9];
		$emailValue = $values[10];
		$programsValue = $values[11];
		$sizeValue = $values[12];
		$condValue = $values[13];
		$uniValue = $values[14];
	}
	//comparo las valildaciones para dar una respuesta
		if( $city == '0' )
			$clasc = "error_val";
		if( $name == '0' )
			$clasn = "error_val";	
		if( $fname == '0' )
			$claspa = "error_val";
		if( $sname == '0' )
			$classa = "error_val";
		if( $user == '0' ){
			$clasu = "error_val";
			$value="Sólo caracteres alfanuméricos o guión bajo.(Mínimo 3)";
		}elseif( $user == '2' ){
			$clasu = "error_val";
			$value="Nombre de usuario no disponible";
		}
		if( $passw == '0' ){
			$clasw = "error_val";
			$valuew="Sólo caracteres alfanuméricos o guión bajo.(6-12)";
		}elseif( $passw == '2' ){
			$claswr = "error_val";
			$valuewr="Debe ingresar el mismo valor";
		}
		if( $tident == '0' )
			$clasi = "error_val";
		if( $ident == '0' )
			$clasd = "error_val";
		if( $genero == '0' )
			$clasg = "error_val";
		if( $dir == '0' )
			$clasdir = "error_val";
		if( $tel== '0' )
			$clast = "error_val";
		if( $email== '0' ){
			$clase = "error_val";
		 	$msgEmail = "Ingresar correo válido";
		}elseif( $email== '2' ){
			$clase = "error_val";
		 	$msgEmail = "Correo ya existente.";
		}
		if( $programs == '0' )
			$clasp = "error_val";
		if( $condiciones == '0' )
			$clascond = "error_val";

}

require(LIBWWWDIR . '/forms.php');
?>

<!DOCTYPE>
<html>
<head>
<title>Register</title>
<link rel="stylesheet" href="../style.css" media="screen">
<!-- css para la fecha, no funciona :(
<link rel="stylesheet" href="../jquery-ui-1.8.16.custom/development-bundle/themes/ui-lightness/jquery-ui-1.8.16.custom.css">-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<!--<script type="text/javascript" src="../jquery-validation-1.9.0/lib/jquery.js" charset="utf-8">
</script>-->
<script type="text/javascript" src="../jquery-validation-1.9.0/lib/jquery-1.3.2.js" charset="utf-8">
</script>
<script type="text/javascript" src="../jquery-validation-1.9.0/jquery.validate.js" charset="utf-8"></script>
<script type="text/javascript" src="../jquery-validation-1.9.0/additional-methods.js" charset="utf-8">
</script>
<!-- librerias para la fecha :(-->
<script type="text/javascript" src="../jquery-ui-1.8.16.custom/js/jquery-1.6.2.min.js" ></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js">
</script>
<!--<script type="text/javascript">-->
<script language="javascript">
$(document).ready(function(){
	var varjs="regist";
$("#"+varjs).validate({
	//evenst able!!
	rules:{
		ciudad : {
            		required: function(element) {
                return $("#ciudad").val() == '';
            	}},
		univ : {
            		required: function(element) {
                return $("#univ").val() == '';
            	}},
		name :{ minlength: 4, lettersonly: true, required: true },
		papell:{ minlength: 4, lettersonly: true, required: true},
		sapell:{ minlength: 4, lettersonly: true, required: false},
		user:{minlength: 3, alphanumeric: true, required: true},
		passw:{minlength: 6, maxlength: 12, required: true},
		passw_r:{equalTo:"#passw", required: true},
		tipo_ident:{
            		required: function(element) {
                return $("#tipo_ident").val() == '';
            	}},
		ident:{ number: true, minlength: 8, required: false},
		genero:{
            		required: function(element) {
                return $("#genero").val() == '';
            	}},
		direc:{ rangelength: [10, 40] },
		tel:{ number: true, rangelength: [7, 15]},
		email:{email: true, required: false},
		programas:{ rangelength: [6, 100 ] },
		condiciones:{ rangelength: [6, 100 ] }
	},
	messages: {
		ident:{ minlength: jQuery.format("Enter at least {0} numbers")},
		tel:{ rangelength: jQuery.validator.format("Enter a value between {0} and {1}")}
	}
	});
});
$(document).ready(function() {
   $('#fecha').datepicker();
});
//otra forma de malipular los valores con valid()
$(document).ready(function(){
	$("#ciudad").change(function(event){
		var id = $("#ciudad").find(':selected').val();
		//alert(id);
		$("#univ").load('genera-select.php?id='+id);
		//alert(id);
	});});

</script>
	
</head>
<body>
<!--<?php include("menu.php"); ?>-->
<h1>Register</h1>
<p>Llena los siguientes campos para registrarte. <span style="color: red; font-size: 10pt"> * </span>Campos requeridos:</p>
<?php echo addForm('newuser.php','post','regist'); ?>
<table>
<tr><td><label for="ciudad">Ciudad<span style="color: red; font-size: 10pt"> *</span>:</td><td>
<?php 
$ciudades= array('' => '...', '1' => 'Armenia', '2' =>'Barrancabermeja',
'3' =>'Barranquilla', '4' =>'Bogotá D.C', '5' =>'Bucaramanga', '6' =>'Cali', '7' =>'Cartagena',
'8' =>'Girardot', '9' =>'Ibagué', '10' =>'Manizales', '11' =>'Medellín', '12' =>'Ocaña', '13' =>'Pamplona', '14' =>'Pereira', '15' =>'Popayán', '16' =>'San Gil', '17' =>'Tulúa' );
/*echo addSelect('ciudad', $ciudades, 'domjudge-must-select', true); */
	$re ='<select name="ciudad" id="ciudad">';
	foreach($ciudades as $k => $v){
		$re .='<option value="'.$k.'"' .
			(($cityValue == $k) ? ' selected="selected"' : '') . '>' .
			$v."</option>\n";
	}
	$re .= "</select>";
	echo $re; ?> 
<span class="<?php echo $clasc?>" style="color: red; font-size: 10pt" >Elegir una ciudad</td></tr>
<tr><td><label for="univ">Universidad:</td><td> 
<select name="univ" id="univ">
<option value="cero"><?php echo $uniValue?></option>
<!--<option value="cero">...</option>-->
</select> 

</tr>
<tr><td><label for="name">Nombre<span style="color: red; font-size: 10pt"> *</span>:</td><td>
<input id="name" type="text" name="name" class="text" value="<?php echo $nameValue?>"/>
<span class="<?php echo $clasn?>" style="color: red; font-size: 10pt" />A-z, Mínimo 4 caracteres</td></tr>
<tr><td><label for="papell">Primer Apellido<span style="color: red; font-size: 10pt"> *</span>:</td>
<td><input id="papell" type="text" name="papell" class="text" value="<?php echo $papellValue?>"/>
<span class="<?php echo $claspa?>" style="color: red; font-size: 10pt"/>A-z, Mínimo 4 caracteres</td></tr>
<tr><td><label for="sapell">Segundo Apellido:</td><td>
<input id="sapell" type="text" name="sapell" class="text" value="<?php echo $sapellValue?>">
<span class="<?php echo $classa?>" style="color: red; font-size: 10pt"/>A-z, Mínimo 4 caracteres</td></tr>
<tr><td><label for="user">Usuario<span style="color: red; font-size: 10pt"> *</span>:</td><td>
<input id="user" type="text" name="user" class="text" value="<?php echo $usernameValue?>"/>
<span id="msgbox" class="<?php echo $clasu?>" style="color: red; font-size: 10pt" ><?php echo $value?>
</span></td></tr>
<tr><td><label for="passw">Contraseña<span style="color: red; font-size: 10pt"> *</span>:</td>
<td><input id="passw" type="password" name="passw"/>
<span class="<?php echo $clasw?>" style="color: red; font-size: 10pt" ><?php echo $valuew ?></td></tr>
<tr><td><label for="passw_r">Verificar contraseña<span style="color: red; font-size: 10pt"> *</span>:</td>
<td><input id="passw_r" type="password" name="passw_r"/>
<span class="<?php echo $claswr ?>" style="color: red; font-size: 10pt" ><?php echo $valuewr ?></td></tr>
<tr><td><label for="tipo_ident">Tipo de identificación<span style="color: red; font-size: 10pt"> *</span>:
</td><td>
<?php 
//$tipos = array( '' => '...', 'TI' => 'TI', 'CC' => 'CC', 'CE' => 'CE', 'PA' => 'PA' );
$tipos = array( '' => '...', 'TI' => 'TI', 'CC' => 'CC');
//echo addSelect('tipo_ident', $tipos, 'cero', true);
	$rp ='<select name="tipo_ident" id="tipo_ident">';
	foreach($tipos as $k => $v){
		$rp .='<option value="'.$k.'"' .
			(($tipoidenValue == $k) ? ' selected="selected"' : '') . '>' .
			$v."</option>\n";
	}
	$rp .= "</select>";
	echo $rp; ?> 
<span class="<?php echo $clasi ?>" style="color: red; font-size: 10pt" >Elegir tipo de identificación
</td></tr>
<tr><td><label for="ident">Identificación<span style="color: red; font-size: 10pt"> *</span>:</td>
<td><input id="ident" type="text" name="ident" value="<?php echo $identifValue ?>"/>
<span class="<?php echo $clasd ?>" style="color: red; font-size: 10pt"/>sólo números, Mínimo 8</td></tr>
<tr><td><label for="fecha">Fecha de nacimiento(validar)<span style="color: red; font-size: 10pt"> *</span>:</td>
<td><input id="fecha" type="textbox" name="fecha"  />
</td></tr>
<tr><td><label for="genero">Género<span style="color: red; font-size: 10pt"> *</span>:
</td><td>
<?php $generos = array( '' => '...', 'MASCULINO' => 'MASCULINO', 'FEMENINO' => 'FEMENINO' );
//echo addSelect('genero', $generos, 'cero', true);
	$ret ='<select name="genero" id="genero">';
	foreach($generos as $k => $v){
		$ret .='<option value="'.$k.'"' .
			(($tipoidenValue == $k) ? ' selected="selected"' : '') . '>' .
			$v."</option>\n";
	}
	$ret .= "</select>";
	echo $ret; ?> 
<span class="<?php echo $clasg ?>" style="color: red; font-size: 10pt" >Elegir género</td>
</tr><tr><td><label for="direc">Dirección:</td><td>
<input id="direc" type="text" name="direc" value="<?php echo$dirValue ?>"/>
<span class="<?php echo $clasdir?>" style="color: red; font-size: 10pt">La longitud debe estar entre 10 y 20 </td><tr><tr><td>
<label for="tel">Teléfono:</td><td><input id="tel" type="text" name="tel" value="<?php echo $telValue ?>"/>
<span class="<?php echo $clast ?>" style="color: red; font-size: 10pt"/>sólo números, Mínimo 7</td></tr>
<tr><td><label for="email:">Correo electrónico<span style="color: red; font-size: 10pt"> *</span>:</td><td>
<input id="email" type="text" name="email" value="<?php echo$emailValue ?>"/>
<span class="<?php echo $clase ?>" style="color: red; font-size: 10pt"/><?php echo $msgEmail ?></td></tr>
<tr><td><label for="programas">Programas Académicos:</td><td>
<textarea name="programas" rows="3" cols="80" id="condiciones" ><?php echo $programsValue?></textarea>
<span class="<?php echo $clasp?>" style="color: red; font-size: 10pt"/>La longitud debe estar entre 6 y 100  </td></tr>
<tr><td><label for="talla">Talla de camiseta:</td><td>
<?php 
$tallas = array( '' => '...', 'XS' => 'XS', 'S' => 'S', 'M' => 'M', 'L' => 'L', 'XL' => 'XL',
 'XXL' => 'XXL', );
//echo addSelect('talla', $tallas, 'cero', true); 
	$r ='<select name="talla" id="talla">';
	foreach($tallas as $k => $v){
		$r .='<option value="'.$k.'"' .
			(($sizeValue == $k) ? ' selected="selected"' : '') . '>' .
			$v."</option>\n";
	}
	$r .= "</select>";
	echo $r; ?> 
</td></tr>
<tr><td><label for="condiciones">Condiciones Especiales:</td><td>
<textarea name="condiciones" rows="3" cols="80" id="condiciones" ><?php echo $condValue?></textarea>
<span class="<?php echo$clascond?>" style="color: red; font-size: 10pt"/>La longitud debe estar entre 6 y 100 
</td></tr>




<tr><td><label for="foto">Foto:(validar)</td><td>
<?php echo addFileField('Seleccionar archivo') ;?>
</td></tr>
<tr><td><label for="submit"></td><td>
<?php echo addSubmit('Registrar', 'submit', "regist.submit()"); ?></td></tr>
<!--<td><button id="submit" value="Registrar" onClick="regist.submit()" > Registrar</button></td></tr>-->
</table>
<?php echo addEndForm(); 
//si todos los campos son validos 
if( isset( $_GET['rf'] ) ){
	if( $_GET['rf'] == '1' ){ ?>
		<script type="text/javascript">
			alert(" Datos guardados exitosamente");
		</script>
<?php }
}?>
</body>
</html>
